package com.java110.mall.cart.bmo.storeOrderUp.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.storeOrderUp.ISaveStoreOrderUpBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;

import com.java110.dto.user.UserDto;
import com.java110.intf.cart.IStoreOrderCartUpInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderUpInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.storeOrder.StoreOrderCartUpPo;
import com.java110.po.storeOrder.StoreOrderUpPo;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("saveStoreOrderUpBMOImpl")
public class SaveStoreOrderUpBMOImpl implements ISaveStoreOrderUpBMO {

    @Autowired
    private IStoreOrderUpInnerServiceSMO storeOrderUpInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO iUserInnerServiceSMOImpl;
    @Autowired
    private IStoreOrderCartUpInnerServiceSMO iStoreOrderCartUpInnerServiceSMOImpl;

    /**
     * 添加小区信息
     *
     * @param
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(JSONObject storeOrderUp,JSONArray productStocks) {


        StoreOrderUpPo storeOrderUpPo = BeanConvertUtil.covertBean(storeOrderUp, StoreOrderUpPo.class);
        UserDto userDto = new UserDto();
        userDto.setUserId( storeOrderUp.getString( "userId" ) );
        List<UserDto> userDtoList = iUserInnerServiceSMOImpl.queryUsers( userDto );
        if(null != userDtoList && userDtoList.size() > 0){
            storeOrderUpPo.setPersonId( userDtoList.get( 0 ).getUserId() );
            storeOrderUpPo.setPersonName( userDtoList.get( 0 ).getName() );
        }
        storeOrderUpPo.setOrderUpId(GenerateCodeFactory.getGeneratorId("11"));
        storeOrderUpPo.setRemark( "商户挂单，系统自带备注" );
        int flag = storeOrderUpInnerServiceSMOImpl.saveStoreOrderUp(storeOrderUpPo);
        //保存挂单明细
        if (flag > 0) {
            for(int productOrderindex=0;productOrderindex<productStocks.size();productOrderindex++){
                JSONObject StoreOrderCartUp = productStocks.getJSONObject( productOrderindex );
                StoreOrderCartUp.put( "cartNum" ,StoreOrderCartUp.getString( "count" ));
                StoreOrderCartUp.put( "price" ,StoreOrderCartUp.getString( "prodPrice" ));
                StoreOrderCartUpPo StoreOrderCartUps =  BeanConvertUtil.covertBean(StoreOrderCartUp, StoreOrderCartUpPo.class);
                StoreOrderCartUps.setOrderUpId( storeOrderUpPo.getOrderUpId() );
                StoreOrderCartUps.setPersonId( storeOrderUpPo.getPersonId() );
                StoreOrderCartUps.setRemark( "商户挂单，系统自带备注" );
                StoreOrderCartUps.setCartUpId(GenerateCodeFactory.getGeneratorId("11"));
                int orderUpFlag =  iStoreOrderCartUpInnerServiceSMOImpl.saveStoreOrderCartUp( StoreOrderCartUps );

                if(orderUpFlag <= 0){
                    return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存挂单明细失败");
                }
            }

            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

}
